package com.docmanage.am.service.impl;

import com.docmanage.am.domain.AmAuthUser;
import com.docmanage.am.domain.AmFolder;
import com.docmanage.am.mapper.AmAuthUserMapper;
import com.docmanage.am.mapper.AmFolderMapper;
import com.docmanage.common.utils.SecurityUtils;
import com.docmanage.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 按用户授权Service业务层处理
 * 
 * @author pointLion
 * @date 2023-07-10
 */
@Service
public class AmAuthUserService
{
    @Autowired
    private AmAuthUserMapper amAuthUserMapper;
    @Autowired
    private AmFolderMapper amFolderMapper;
    @Autowired
    private AmFolderService amFolderService;

    /**
     * 查询按用户授权
     * 
     * @param id 按用户授权主键
     * @return 按用户授权
     */
    public AmAuthUser selectAmAuthUserById(Long id)
    {
        return amAuthUserMapper.selectAmAuthUserById(id);
    }

    /**
     * 查询按用户授权列表
     * 
     * @param amAuthUser 按用户授权
     * @return 按用户授权
     */
    public List<AmAuthUser> selectAmAuthUserList(AmAuthUser amAuthUser)
    {
        return amAuthUserMapper.selectAmAuthUserList(amAuthUser);
    }

    /**
     * 新增按用户授权
     * 
     * @param amAuthUser 按用户授权
     * @return 结果
     */
    public int insertAmAuthUser(AmAuthUser amAuthUser)
    {
        return amAuthUserMapper.insertAmAuthUser(amAuthUser);
    }

    /**
     * 修改按用户授权
     * 
     * @param amAuthUser 按用户授权
     * @return 结果
     */
    public int updateAmAuthUser(AmAuthUser amAuthUser)
    {
        return amAuthUserMapper.updateAmAuthUser(amAuthUser);
    }

    /**
     * 批量删除按用户授权
     * 
     * @param ids 需要删除的按用户授权主键
     * @return 结果
     */
    public int deleteAmAuthUserByIds(Long[] ids)
    {
        return amAuthUserMapper.deleteAmAuthUserByIds(ids);
    }

    /**
     * 删除按用户授权信息
     * 
     * @param id 按用户授权主键
     * @return 结果
     */
    public int deleteAmAuthUserById(Long id)
    {
        return amAuthUserMapper.deleteAmAuthUserById(id);
    }




    public List<AmAuthUser> listAuthFolder(Long userId){
        return amAuthUserMapper.listAuthFolder(userId);
    }




    /*****
     * 给文件夹授权
     */
    public void allotFolder(AmAuthUser authUser){
        //公共属性
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        String type = "0";
        authUser.setType(type);
        authUser.setOperateTime(date);
        authUser.setOperateBy(userId);
        //如果之前就有---先操作自己
        if(authUser.getId()!=null){
            amAuthUserMapper.updateAmAuthUser(authUser);
        }else {
            amAuthUserMapper.insertAmAuthUser(authUser);
        }
        // 如果范围是影响下级所有
        if("1".equals(authUser.getRange())){
            // 先删除当前等级下的所有权限
            Long folderId = authUser.getFolderFileId();
            AmFolder folder = amFolderMapper.selectAmFolderById(folderId);
            String cpath = (StringUtils.isNotBlank(folder.getCpath())?folder.getCpath():"") +"/"+folder.getId();
            amAuthUserMapper.deleteByAllotFolder(cpath);
            // 再批量插入新的数据
            List<AmFolder> folderList =  amFolderService.selectAllChildren(folder);
            List<AmAuthUser> batchList = new ArrayList<>();
            for(AmFolder f : folderList){
                AmAuthUser i = new AmAuthUser();
                i.setOperateBy(SecurityUtils.getUserId());
                i.setOperateTime(date);
                i.setUserId(authUser.getUserId());
                i.setType(type);
                i.setRange("0");//孩子都是自己
                i.setAuthView(authUser.getAuthView());
                i.setAuthAdd(authUser.getAuthAdd());
                i.setAuthUpdate(authUser.getAuthUpdate());
                i.setAuthViewFileInFolder(authUser.getAuthViewFileInFolder());
                i.setAuthDelete(authUser.getAuthDelete());
                i.setFolderFileId(f.getId());
                i.setAuthDelete(authUser.getAuthDelete());
                batchList.add(i);
            }
            if(folderList.size()>0){
                amAuthUserMapper.batchInsert(batchList);
            }
        }
    }
}
